package com.apache.rpc.client;

import com.apache.api.vo.ResultEntity;
import com.apache.database.model.Page;
import com.apache.rpc.common.JsonEntityParese;
import com.apache.rpc.common.XMLMapParse;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.entity.Protocol;
import com.apache.tools.StrUtil;
import net.sf.json.JSONObject;
import org.codehaus.jackson.map.ObjectMapper;

import java.util.Map;

/**
 * description: 协议父类
 *
 * @author dyh 创建时间：2018年01月22日
 */
public abstract class AbstractProtocolService {

    public abstract ResultEntity doService(String beanId, String methodKey,
            Map<String, Object> params);

    public abstract String doServiceStr(String beanId, String methodKey,
            Map<String, Object> params);

    protected InterfaceRegister register;

    protected String httpUrl;//访问地址

    protected String httpIp;//访问IP

    protected String httpProt;//访问端口

    private boolean isSendBody=false;

    public void setRegister(String httpIp, String httpProt, String httpUrl) {
        this.register = new InterfaceRegister();
        register.setAddress(StrUtil.doNull(httpIp, httpUrl));
        register.setPort(StrUtil.doNull(httpProt, ""));
    }

    protected String buildXml(String beanId, String methodKey, String protocolType,
            Map<String, Object> params) {
        Protocol pt = new Protocol();
        pt.setBeanId(beanId);
        pt.setReqFlag(methodKey);
        pt.setProtocolType(protocolType);
        String paramType = StrUtil.doNull(String.valueOf(params.get("ParamType")), "xml");//返报文类型
        pt.setRespType(paramType);
        params.remove("ParamType");
        pt.setParams(params);
        return XMLMapParse.instance().buildSingleXML(pt);
    }

    protected ResultEntity getResultEntity(String message) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setResult("false");
        resultEntity.setMessage(message);
        //resultEntity.setEntity("");
        return resultEntity;
    }

    public void setRegister(InterfaceRegister register) {
        this.register = register;
    }

    protected ResultEntity getResultEntity(String content, String paramType, String isPage) {
        ResultEntity resultEntity = null;
        if (StrUtil.isNull(content)) {
            resultEntity = getResultEntity("接口调用超时");
            return resultEntity;
        }
        if ("json".equalsIgnoreCase(paramType)) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                resultEntity = objectMapper.readValue(content, ResultEntity.class);
                if (StrUtil.isNotNull(isPage)) {
                    JSONObject json = JSONObject.fromObject(content);
                    Page page = objectMapper.readValue(json.getString("entity"), Page.class);
                    resultEntity.setEntity(page);
                }
            } catch (Exception e) {
                resultEntity = getResultEntity("响应报文转换错误:" + e.getMessage());
                System.out.println("响应报文>>" + content);
                return resultEntity;
            }
        } else {
            resultEntity = JsonEntityParese.instance().toBean(content, ResultEntity.class);
        }
        return resultEntity;
    }

    protected boolean isSendBody() {
        return isSendBody;
    }

    public void setSendBody(boolean sendBody) {
        isSendBody = sendBody;
    }
}
